<?php
namespace Addons\CustomerQrcode\Controller\Admin;

use Models\BaseModel as Model;
use Addons\CustomerQrcode\Helper;
use Addons\Fission\Helper as Helperf;
use Addons\QyWechat\Helper as Helpers;
use Session;

class CustomerQrcode extends BaseAdmin
{	
	public function index(){
		$wid = Session::get('wid');
		$h = Helper::getInstance();
		// $replyConfig = (new Model('qy_reply_config'))->find(['wid'=>$wid]);
		$replyConfig = Helpers::getInstance()->get_message_send_config($wid);
		$qyConfig = (new Model('qy_wechat_config'))->find(['wid'=>$wid]);
		$toolbarUrl = $h->get_http_server_name().'/Addons/CustomerQrcode/H5/CustomerQrcode.html?corp_id='.$qyConfig->corp_id.'&agent_id='.$replyConfig->agent_id;
		$this->assign("toolbarUrl", $toolbarUrl);
		return $this->show();
	}

	public function qrcode_list(){
		$wid = Session::get('wid');
		$h = Helpers::getInstance(); 
		// $r_user_id_arr = $h->get_check_range('et_data');
		// if($r_user_id_arr){
		// 	$where['user_id'] = $r_user_id_arr;
		// }
		$where["wid"] = $wid;
		$p = new \Pagination();		
		$lists = new Model("qy_et_qrcode");
		$lists = $p->model_list($lists->where($where)->order('id desc'),true);
		foreach ($lists as $key => $value) {
			$lists[$key]["user"] = (new Model("qy_externalcontact_list"))->field("name,avatar")->find(["external_userid"=>$value["et_userid"],"user_id"=>$value["user_id"]])->get_model_array();
			$lists[$key]["qy_user"] = (new Model("qy_user_list"))->field("name,avatar")->find($value["user_id"])->get_model_array();
		}
		$this->assign("p", $p);
		$this->assign("P", $GLOBALS['P']);
		$this->assign("lists", $lists);
		return $this->show();
	}

	public function details(){
		$id = \Request::get("id");
		$list = (new Model("qy_et_qrcode"))->find($id);
		$p = new \Pagination();	
		$where["qrcode_id"] = $id;
		$lists = $p->model_list((new Model("qy_et_qrcode_invite"))->where($where)->order('id desc'),true);
		foreach ($lists as $key => $value) {
			$lists[$key]["user"] = (new Model("qy_externalcontact_list"))->field("name,avatar")->find(["external_userid"=>$value["et_userid"],"user_id"=>$list->user_id])->get_model_array();
		}
		$this->assign("id", $id);
		$this->assign("p", $p);
		$this->assign("P", $GLOBALS['P']);
		$this->assign("lists", $lists);
		return $this->show();
	}
}